package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.ProcessWorkstationResponse;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.*;
import com.mesSystem.entity.Process;
import com.mesSystem.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("order/increase/productionArrange")
public class ProductionArrangeController {
    @Autowired
    ProductionArrangeService productionArrangeService;
    @Autowired
    ProcessWorkstationService processWorkstationService;
    @Autowired
    ProcessRouteService processRouteService;


    /**
     * 随着销售订单的创建，自动生成 生产安排单
     * 根据订单id获取产品id
     * 根据产品id获取工艺路线信息
     * @param productionArrange
     * @return
     */
//    @PostMapping("increase")
    public Result increase(@RequestBody ProductionArrange productionArrange) {
        log.info(productionArrange.toString());
        // 存入数据库
        productionArrangeService.save(productionArrange);
        return Result.ok("创建成功");
    }

    /**
     * 根据工艺路线id显示工序信息
     */
    @GetMapping("getProcess")
    public Result getProcess(Long processRouteId) {
        // 获得工序信息
        List<Process> processes = processRouteService
                .queryProcessByProcessRouteId(processRouteId);
        return Result.ok(processes);
    }

    /**
     * 更具工序id查询工位类型
     */
    @GetMapping("getProcess/getWorkstationType")
    public Result getWorkstationType(Long processId) {
        // 获得工位类型及数量
        List<ProcessWorkstationResponse> workstationTypes = processWorkstationService
                .queryWorkstationByProcessId(processId);

        return Result.ok(workstationTypes);
    }


    /**
     * 删除生产安排单
     * TODO---根据订单删除相关内容
     *
     * @param
     * @return
     */
    @DeleteMapping("delete")
    public Result delete(Long productionArrangeId) {
        productionArrangeService.removeById(productionArrangeId);
        return Result.ok("删除成功");
    }

    /**
     * 分页查询信息
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        // 创建分页器对象
        Page<ProductionArrange> Page = new Page<>(page, size);

        // 调用分页查询方法，将分页器传递给service
        productionArrangeService.page(Page, new QueryWrapper<ProductionArrange>().orderByAsc("production_id"));

        // 从分页器对象中获取分页后的数据
        List<ProductionArrange> list = Page.getRecords();

        log.info("被查询信息为：{}", list);

        // 返回分页结果
        return Result.ok(list);
    }

    /**
     * 更改
     */
    @PostMapping("update")
    public Result update(@RequestBody ProductionArrange productionArrange) {
        productionArrangeService.updateById(productionArrange);
        log.info("update:{}", productionArrange);
        return Result.ok("更新成功");
    }
}
